package edu.neusoft.android.yuejian14.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import java.util.List;

import edu.neusoft.android.yuejian14.Listener.RetrofitListener;
import edu.neusoft.android.yuejian14.R;
import edu.neusoft.android.yuejian14.activity.AddPicActivity;
import edu.neusoft.android.yuejian14.adapter.PicListAdapter;
import edu.neusoft.android.yuejian14.javabean.Pic;
import edu.neusoft.android.yuejian14.model.PicModel;

public class PicFragment extends Fragment implements View.OnClickListener,RetrofitListener
{
	private RecyclerView recyclerView;
	private PicListAdapter picListAdapter;
	private ImageButton add;


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.tab04, container, false);
		initData();//网络获取
		return initView(view);

	}

	private View initView(View view) {

		recyclerView = (RecyclerView) view.findViewById(R.id.home_recycler);
		add = (ImageButton) view.findViewById(R.id.btn_add);
		add.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), AddPicActivity.class);
				startActivity(intent);
			}
		});

		return view;
	}

	private void initData() {
		PicModel picModel = new PicModel();
		picModel.cardlist(this);
	}
	@Override
	public void onSuccess(Object object, int flag) {
		if (getActivity() != null){
			List<Pic> pic = (List<Pic>) object;
			picListAdapter = new PicListAdapter(getActivity(),pic);
			LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
			layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
			recyclerView.setLayoutManager(layoutManager);
			recyclerView.setAdapter(picListAdapter);
			recyclerView.setItemAnimator(new DefaultItemAnimator());
		}
	}

	@Override
	public void onFail() {
	}

	@Override
	public void onClick(View view) {
	}
}
